#include <Arduino.h>
#include "sqlite3.h"
#include <SD.h>
#include <SD_MMC.h>
#include <FS.h>
#include <SPIFFS.h>
#include <stdio.h>
#include <RTC_RX8025.h>
#include <Wire.h>
#include <WiFi.h>
#include "ads1110.h"
#include "hx712.h"

#define SD_SCK  17
#define SD_MOSI 18
#define SD_MISO 16
#define SD_CSS  15

#define IIC_SDA 8
#define IIC_SCL 19

#define BEEP_PIN 12

#define HX712_DIO 20
#define HX712_SCK 3

#define WIFI_NAME "LINKTA_2"
#define WIFI_PASS "linkta2022"

const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

SPIClass spi(HSPI);
RX8025 rtc;
com_ads1110_dev_t ads1110;
HX712 scale; 

//ADS1110写函数
int ads1110a0_write(uint8_t dev_addr, uint8_t *pdata, uint8_t size)
{
    Wire.beginTransmission(dev_addr);
    if(Wire.write(pdata,size) == size){
        Wire.endTransmission();
        return COM_SUCCESS;
    }
    else{
        Wire.endTransmission();
        return COM_ERROR_SEND_FAILED;
    }
}

//ADS1110读函数
int ads1110a0_read(uint8_t dev_addr, uint8_t *pdata, uint8_t size)
{
    int i = 0;

    Wire.requestFrom(dev_addr,size);

    while (Wire.available())
        pdata[i++] = Wire.read();
    
    if(i == size)
        return COM_SUCCESS;
    else
        return COM_ERROR_RECV_FAILED;
}

int ads1110a0_get_realT(float* v)
{
	int err;
    static float va[5];
    float tv;
	err = com_ads1110_init(&ads1110,COM_DEV_ADS1110_ADDR_A0,ads1110a0_write,ads1110a0_read);
	if(err != COM_SUCCESS) return err;
	err = com_ads1110_set_mode(&ads1110,COM_DEV_ADS1110_MODE_CONTINU);
	if(err != COM_SUCCESS) return err;
	err = com_ads1110_write_cfg_reg(&ads1110);
	if(err != COM_SUCCESS) return err;

	com_ads1110_read_voltage(&ads1110,&tv);
	com_ads1110_read_voltage(&ads1110,&tv);
	com_ads1110_read_voltage(&ads1110,&tv);
	com_ads1110_read_voltage(&ads1110,&tv);
	com_ads1110_read_voltage(&ads1110,&tv);//连续读5个值等待稳定

    for (size_t i = 0; i < 5; i++){
        err = com_ads1110_read_voltage(&ads1110,&tv);
        if(err != COM_SUCCESS) return err;
        va[i] = tv;
    }
    *v = (va[0]+va[1]+va[2]+va[3]+va[4])/5;

	err = com_ads1110_set_mode(&ads1110,COM_DEV_ADS1110_MODE_SINGLE);
	if(err != COM_SUCCESS) return err;
	err = com_ads1110_write_cfg_reg(&ads1110);	//设置ADS1110为单次模式，减少电量损耗
	if(err != COM_SUCCESS) return err;

    return COM_SUCCESS;
}

void setup() {
    Serial.begin(115200);

    delay(3000);

    WiFi.begin(WIFI_NAME,WIFI_PASS);
    Serial.println("WIFI connect");
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");

    spi.begin(SD_SCK,SD_MOSI,SD_MISO,SD_CSS);
    if (!SD.begin(SD_CSS, spi)) {
        Serial.println("Card Mount Failed");
        return;
    }



    Wire.begin(IIC_SDA,IIC_SCL);
    rtc.RX8025_init();

    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    struct tm timeinfo;
    if (!getLocalTime(&timeinfo))
    {
        Serial.println("Failed to obtain time");
        return;
    }

    rtc.setRtcTime(timeinfo.tm_sec,timeinfo.tm_min,timeinfo.tm_hour,
        timeinfo.tm_wday,timeinfo.tm_mday,timeinfo.tm_mon+1,timeinfo.tm_year-100);

    pinMode(BEEP_PIN,OUTPUT);
    digitalWrite(BEEP_PIN,1);
    delay(500);
    digitalWrite(BEEP_PIN,0);

    float v;
    ADS1110_ERR_CHECK(ads1110a0_get_realT(&v));
    Serial.printf("v:%.2fv\n",v/20*140/1000);

    scale.begin(HX712_DIO,HX712_SCK);
}

void loop() {
    // long reading = scale.read();
    Serial.print("HX712 reading: ");
    // Serial.println(reading);
    Serial.print(scale.readReal());
    Serial.print("g,state ");
    Serial.println(scale.state());
    delay(100);
}
